package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @ClassName RoleController
 * @Description TODO
 * @Author sy
 * @Date 2019/12/12 13:09
 * @Version 1.0
 **/
@RestController
@RequestMapping("role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("findAll")
    public Result findAll(){
        List<Role> roleList = roleService.findAll();
        if(roleList != null && roleList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_ROLE_SUCCESS);
            result.setData(roleList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result=roleService.findPage(queryPageBean);
        return result;
    }

    @RequestMapping("findAllMap")
    public Result  findAllMap(){
        try {
            Map<String,Object> map= roleService.findAllPage();
           return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Map<String,Object> map){
        try {
            roleService.add(map);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if(role != null){
            Result result = new Result(true, MessageConstant.QUERY_ROLE_SUCCESS);
            result.setData(role);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
    }
    @RequestMapping("findmenuAndPermissionIdsByRoleId")
    public Result findmenuAndPermissionIdsByRoleId(Integer id){
        try {
            HashMap<String,Object> map= roleService.findmenuAndPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }

    }

    //删除检查项
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            roleService.deleteById(id);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Map<String,Object> map){
        try {
            roleService.edit(map);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }
}
